package cvi.com.reader.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.base.BaseActivity;
import cvi.com.reader.bean.constant;
import cvi.com.reader.network.DefaultObserver;
import cvi.com.reader.network.NetWorkManager;
import cvi.com.reader.tool.UtilsOnclickListen;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 梅涛 on 2020/11/19.
 */

public class UpdataNameActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tittle)
    TextView tittle;
    @BindView(R.id.sava_tv)
    TextView savaTv;
    @BindView(R.id.right_icon)
    LinearLayout rightIcon;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.clear_ll)
    LinearLayout clearLl;
    private String reName = "";

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.clear_ll:
                name.setText("");
                break;
            case R.id.sava_tv:
                if (UtilsOnclickListen.isFastClick()) {
                    SaveData();
                }
                break;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_updata_name;
    }

    @Override
    protected void initView() {
        savaTv.setOnClickListener(this);
        clearLl.setOnClickListener(this);
        back.setOnClickListener(this);
        tittle.setText("修改姓名");
        reName = getIntent().getStringExtra("name");
        if (reName.isEmpty()) {
            name.setText("");
        } else {
            name.setText(reName);
        }

    }

    private void SaveData() {
        if (name.getText().toString().trim().isEmpty()) {
            Toast.makeText(UpdataNameActivity.this, "内容不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }
        if (name.getText().toString().trim().length() < 2) {
            Toast.makeText(UpdataNameActivity.this, "长度不能少于两位！", Toast.LENGTH_SHORT).show();
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("name", name.getText().toString().trim());
        //党支部接口
        NetWorkManager.getInstance().getNetWorkApiService().updataPersonInfo(map).compose(this.<JSONObject>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<JSONObject>(UpdataNameActivity.this) {
                    @Override
                    protected void onSuccess(JSONObject response) {
                        if (response != null) {
                            Toast.makeText(UpdataNameActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent();
                            intent.setAction(constant.UPDATA_PARTY_MENBER_INFO_MYRECEIVER);
                            sendBroadcast(intent);
                            finish();
                        }

                    }
                });

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void extrasDoSome(Bundle extras) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
